﻿using System.Web.Mvc;
using Beeldbankmvc.Models;
using System.Collections.Generic;
using System;
using Beeldbankmvc.Code;
using Beeldbankmvc.ViewModels;
using System.Globalization;
using System.Linq;
using System.Configuration;

namespace Beeldbankmvc.Controllers
{
    public class APIController : Controller
    {
        #region W8_TopMediaList
        public JsonResult W8_TopMediaList(string searchString, int? typeId = Constants.ITEM_TYPE_DIGITAL, int? aantalResultaten = 600000, byte? orderBy = 7)
        {
            if (ConfigurationManager.AppSettings["OpenAPI"] == "true")
            {
                List<JsonMetroViewModel> resultItems = new List<JsonMetroViewModel>();

                SearchController searchController = new SearchController();
                MetroSearchViewModel viewModel = null;

                viewModel = searchController.metroTopMediaList(searchString, typeId.Value, aantalResultaten.Value, orderBy.Value);


                StarRatingRepository starRatingRep = new StarRatingRepository();


                foreach (var mediaObject in viewModel.mediaobjectCollections)
                {
                    String themeImageName = Beeldbankmvc.Code.Utility.GetSiteRoot() + "appData/" + mediaObject.CollectionId.ToString() + ".jpg";
                    String mediaImageName = Beeldbankmvc.Code.Utility.GetSiteRoot() + "MediaService/" + mediaObject.FileNumber;

                    JsonThemeResultsViewModel groupData = new JsonThemeResultsViewModel()
                    {
                        CollectionId = mediaObject.CollectionId,
                        CollectionName = mediaObject.CollectionName,
                        CollectionDescription = mediaObject.CollectionDescription,
                        CollectionCount = 0,
                        CollectionImage = themeImageName
                    };

                    JsonMetroViewModel item = new JsonMetroViewModel()
                    {
                        group = groupData,
                        image = mediaImageName,
                        key = mediaObject.MediaObjectId,
                        title = mediaObject.Title,
                        description = AppHelper.StripHTMLTags(mediaObject.Description.Replace("</p><p>", "\n\n")),
                        alttitle = mediaObject.AltTitle,
                        filenumber = mediaObject.FileNumber,
                        author = mediaObject.Author,
                        keywords = mediaObject.Keywords,
                        location = mediaObject.Location,
                        videoformat = mediaObject.VideoFormat,
                        datecreated = String.Format("{0:d}", mediaObject.DateCreated),
                        rating = starRatingRep.FindOne(mediaObject.MediaObjectId).avg
                    };
                    if (mediaObject.VideoLength.HasValue)
                    {
                        item.videolength = String.Format("{0:T}", mediaObject.VideoLength);
                        if (item.videolength.Length > 8)
                            item.videolength = item.videolength.Substring(0, 8);
                    }
                    if (mediaObject.Latitude.HasValue)
                        item.latitude = mediaObject.Latitude.Value;
                    if (mediaObject.Longitude.HasValue)
                        item.longitude = mediaObject.Longitude.Value;
                    if (mediaObject.Height.HasValue)
                        item.height = mediaObject.Height.Value;
                    if (mediaObject.GoogleZoom.HasValue)
                        item.googlezoom = mediaObject.GoogleZoom.Value;

                    resultItems.Add(item);
                }

                return Json(resultItems, JsonRequestBehavior.AllowGet);
            }
            else
                return null;
        }
        #endregion

        #region W8_MetroSimpleSearch
        public JsonResult W8_MetroSimpleSearch(string searchString, int? typeId = Constants.ITEM_TYPE_DIGITAL, int? aantalResultaten = 250, int skip = 0, byte? orderBy = 7)
        {
            if (ConfigurationManager.AppSettings["OpenAPI"] == "true")
            {
                List<JsonMetroViewModel> resultItems = new List<JsonMetroViewModel>();

                SearchController searchController = new SearchController();
                MetroSearchViewModel viewModel = null;

                int page = skip / Constants.SEARCH_PAGE_SIZE;
                viewModel = searchController.metroSimpleSearch(searchString, typeId.Value, page);
              
                StarRatingRepository starRatingRep = new StarRatingRepository();

                foreach (var mediaObject in viewModel.mediaobjectCollections)
                {
                    String themeImageName = Beeldbankmvc.Code.Utility.GetSiteRoot() + "appData/" + mediaObject.CollectionId.ToString() + ".jpg";
                    String mediaImageName = Beeldbankmvc.Code.Utility.GetSiteRoot() + "MediaService/" + mediaObject.FileNumber;

                    JsonThemeResultsViewModel groupData = new JsonThemeResultsViewModel()
                    {
                        CollectionId = mediaObject.CollectionId,
                        CollectionName = mediaObject.CollectionName,
                        CollectionDescription = mediaObject.CollectionDescription,
                        CollectionCount = 0,
                        CollectionImage = themeImageName
                    };

                    JsonMetroViewModel item = new JsonMetroViewModel()
                    {
                        group = groupData,
                        image = mediaImageName,
                        key = mediaObject.MediaObjectId,
                        title = mediaObject.Title,
                        description = AppHelper.StripHTMLTags(mediaObject.Description.Replace("</p><p>", "\n\n")),
                        alttitle = mediaObject.AltTitle,
                        filenumber = mediaObject.FileNumber,
                        author = mediaObject.Author,
                        keywords = mediaObject.Keywords,
                        location = mediaObject.Location,
                        videoformat = mediaObject.VideoFormat,
                        datecreated = String.Format("{0:d}", mediaObject.DateCreated),
                        rating = starRatingRep.FindOne(mediaObject.MediaObjectId).avg
                    };
                    if (mediaObject.VideoLength.HasValue)
                    {
                        item.videolength = String.Format("{0:T}", mediaObject.VideoLength);
                        if (item.videolength.Length > 8)
                            item.videolength = item.videolength.Substring(0, 8);
                    }
                    if (mediaObject.Latitude.HasValue)
                        item.latitude = mediaObject.Latitude.Value;
                    if (mediaObject.Longitude.HasValue)
                        item.longitude = mediaObject.Longitude.Value;
                    if (mediaObject.Height.HasValue)
                        item.height = mediaObject.Height.Value;
                    if (mediaObject.GoogleZoom.HasValue)
                        item.googlezoom = mediaObject.GoogleZoom.Value;

                    item.searchResultSetSize = viewModel.NumberOfResults;

                    resultItems.Add(item);
                }

                return Json(resultItems, JsonRequestBehavior.AllowGet);
            }
            else
                return null;
        }
        #endregion

        #region W8_ThemeMediaList
        public JsonResult W8_ThemeMediaList(int collectionId, int? typeId = Constants.ITEM_TYPE_DIGITAL, int? aantalResultaten = 250, byte? orderBy = 7, int? skip = 0)
        {
            if (ConfigurationManager.AppSettings["OpenAPI"] == "true")
            {
                List<JsonMetroViewModel> resultItems = new List<JsonMetroViewModel>();

                int aantal = 250;
                if (aantalResultaten.HasValue)
                    aantal = aantalResultaten.Value > 250 ? 250 : aantalResultaten.Value;

                SearchController searchController = new SearchController();
                var viewModel = searchController.metroThemeMediaList(collectionId, typeId.Value, aantal + skip.Value, orderBy.Value);

                StarRatingRepository starRatingRep = new StarRatingRepository();


                int counter = 1;
                foreach (var mediaObject in viewModel.mediaobjectCollections)
                {
                    if (counter > skip)
                    {
                        String themeImageName = Beeldbankmvc.Code.Utility.GetSiteRoot() + "appData/" + mediaObject.CollectionId.ToString() + ".jpg";
                        String mediaImageName = Beeldbankmvc.Code.Utility.GetSiteRoot() + "MediaService/" + mediaObject.FileNumber;

                        JsonThemeResultsViewModel groupData = new JsonThemeResultsViewModel()
                        {
                            CollectionId = mediaObject.CollectionId,
                            CollectionName = mediaObject.CollectionName,
                            CollectionDescription = mediaObject.CollectionDescription,
                            CollectionCount = 0,
                            CollectionImage = themeImageName
                        };

                        JsonMetroViewModel item = new JsonMetroViewModel()
                        {
                            group = groupData,
                            image = mediaImageName,
                            key = mediaObject.MediaObjectId,
                            title = mediaObject.Title,
                            description = AppHelper.StripHTMLTags(mediaObject.Description.Replace("</p><p>", "\n\n")),
                            alttitle = mediaObject.AltTitle,
                            filenumber = mediaObject.FileNumber,
                            author = mediaObject.Author,
                            keywords = mediaObject.Keywords,
                            location = mediaObject.Location,
                            videoformat = mediaObject.VideoFormat,
                            datecreated = String.Format("{0:d}", mediaObject.DateCreated),
                            rating = starRatingRep.FindOne(mediaObject.MediaObjectId).avg
                        };
                        if (mediaObject.VideoLength.HasValue)
                        {
                            item.videolength = String.Format("{0:T}", mediaObject.VideoLength);
                            if (item.videolength.Length > 8)
                                item.videolength = item.videolength.Substring(0, 8);
                        }
                        if (mediaObject.Latitude.HasValue)
                            item.latitude = mediaObject.Latitude.Value;
                        if (mediaObject.Longitude.HasValue)
                            item.longitude = mediaObject.Longitude.Value;
                        if ((mediaObject.Height.HasValue) && (mediaObject.Height.Value > 0))
                            item.height = mediaObject.Height.Value;
                        if ((mediaObject.GoogleZoom.HasValue) && (mediaObject.GoogleZoom.Value > 0))
                            item.googlezoom = mediaObject.GoogleZoom.Value;

                        resultItems.Add(item);
                    }
                    counter++;
                }

                return Json(resultItems, JsonRequestBehavior.AllowGet);
            }
            else
                return null;
        }
        #endregion

        #region W8_ThemesList
        public JsonResult W8_ThemesList(int? typeId = Constants.ITEM_TYPE_DIGITAL)
        {
            if (ConfigurationManager.AppSettings["OpenAPI"] == "true")
            {
                List<JsonThemeResultsViewModel> resultItems = new List<JsonThemeResultsViewModel>();

                CollectionRepository repository = new CollectionRepository();
                foreach (var theme in repository.metroThemesList(typeId.Value))
                {
                    JsonThemeResultsViewModel item = new JsonThemeResultsViewModel()
                    {
                        CollectionId = theme.CollectionId,
                        CollectionName = theme.CollectionName,
                        CollectionDescription = theme.CollectionDescription,
                        CollectionCount = theme.CollectionCount,
                        CollectionImage = theme.CollectionImage
                    };
                    resultItems.Add(item);
                }

                return Json(resultItems, JsonRequestBehavior.AllowGet);
            }
            else
                return null;
        }
        #endregion

        #region W8_RateMediaObject
        public JsonResult W8_RateMediaObject(int rating, int mediaObjectId)
        {
            RatingController ratingController = new RatingController();
            return Json(ratingController.Index(rating, mediaObjectId), JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region W8_MediaObject UNUSED
        public JsonResult W8_MediaObject(string Id)
        {
            if (ConfigurationManager.AppSettings["OpenAPI"] == "true")
            {
                MediaObjectRepository mediaObjectRepository = new MediaObjectRepository();

                BEELDBANK_MEDIAOBJECT mediaObject = null;
                try
                {
                    mediaObject = mediaObjectRepository.FindOne(Convert.ToInt32(Id));
                    JsonMediaObjectViewModel item = new JsonMediaObjectViewModel()
                    {
                        Id = mediaObject.Id,
                        Titel = mediaObject.Titel,
                        AltTekst = mediaObject.AltTekst,
                        Omschrijving = AppHelper.StripHTMLTags(mediaObject.Omschrijving.Replace("</p><p>", "\n\n")),
                        Trefwoorden = mediaObject.Trefwoorden,
                        FileNummer = mediaObject.FileNummer.Value,
                        Vervaardiger = mediaObject.Vervaardiger,
                        Lokatie = mediaObject.Lokatie,
                        GPS = AppHelper.getGPSLink(mediaObject),
                        Montage = mediaObject.Montage
                    };
                    if (mediaObject.Datum_Realisatie.HasValue)
                        item.Datum_Realisatie = String.Format("{0:d}", mediaObject.Datum_Realisatie);
                    if (mediaObject.VideoLengte.HasValue)
                    {
                        item.VideoLengte = String.Format("{0:T}", mediaObject.VideoLengte);
                        if (item.VideoLengte.Length > 8)
                            item.VideoLengte = item.VideoLengte.Substring(0, 8);
                    }

                    return Json(item, JsonRequestBehavior.AllowGet);
                }
                catch
                {
                    return null;
                }
            }
            return null;
        }
        #endregion

        #region W8_CreateTileInfo
        public JsonResult W8_CreateTileInfo()
        {
            ReportRepository repository = new ReportRepository();
            return Json(repository.GenerateInfoTiles());
        }
        #endregion

        #region W8_NewMediaList
        public JsonResult W8_NewMediaList(int? typeId = Constants.ITEM_TYPE_DIGITAL, int? aantalResultaten = 250, byte? orderBy = 7, int? skip = 0)
        {
            if (ConfigurationManager.AppSettings["OpenAPI"] == "true")
            {
                List<JsonMetroViewModel> resultItems = new List<JsonMetroViewModel>();

                int aantal = 250;
                if (aantalResultaten.HasValue)
                    aantal = aantalResultaten.Value > 250 ? 250 : aantalResultaten.Value;

                SearchController searchController = new SearchController();
                var viewModel = searchController.metroNewMediaList(aantal + skip.Value, orderBy.Value, typeId.Value);

                int counter = 1;
                foreach (var mediaObject in viewModel.mediaobjectCollections)
                {
                    if (counter > skip)
                    {
                        String mediaImageName = Beeldbankmvc.Code.Utility.GetSiteRoot() + "MediaService/" + mediaObject.FileNumber;

                        JsonThemeResultsViewModel groupData = new JsonThemeResultsViewModel()
                        {
                            CollectionId = 53,
                            CollectionName = "",
                            CollectionDescription = "",
                            CollectionCount = 0,
                            CollectionImage = ""
                        };

                        JsonMetroViewModel item = new JsonMetroViewModel()
                        {
                            group = groupData,
                            image = mediaImageName,
                            key = mediaObject.MediaObjectId,
                            title = mediaObject.Title,
                            description = AppHelper.StripHTMLTags(mediaObject.Description.Replace("</p><p>", "\n\n")),
                            alttitle = mediaObject.AltTitle,
                            filenumber = mediaObject.FileNumber,
                            author = mediaObject.Author,
                            keywords = mediaObject.Keywords,
                            location = mediaObject.Location,
                            videoformat = mediaObject.VideoFormat,
                            datecreated = String.Format("{0:d}", mediaObject.DateCreated)
                        };
                        if (mediaObject.VideoLength.HasValue)
                        {
                            item.videolength = String.Format("{0:T}", mediaObject.VideoLength);
                            if (item.videolength.Length > 8)
                                item.videolength = item.videolength.Substring(0, 8);
                        }
                        if (mediaObject.Latitude.HasValue)
                            item.latitude = mediaObject.Latitude.Value;
                        if (mediaObject.Longitude.HasValue)
                            item.longitude = mediaObject.Longitude.Value;
                        if ((mediaObject.Height.HasValue) && (mediaObject.Height.Value > 0))
                            item.height = mediaObject.Height.Value;
                        if ((mediaObject.GoogleZoom.HasValue) && (mediaObject.GoogleZoom.Value > 0))
                            item.googlezoom = mediaObject.GoogleZoom.Value;

                        resultItems.Add(item);
                    }
                    counter++;
                }

                return Json(resultItems, JsonRequestBehavior.AllowGet);
            }
            else
                return null;
        }
        #endregion

    }
}
